package com.sunzm.common.utils.kafka;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringSerializer;

import java.util.Properties;

public class KafkaProducerDemo {
    public static void main(String[] args) throws InterruptedException {
        String bootstrapServers = "82.156.210.70:9093";
        //服务器上运行，使用内网
        //String bootstrapServers = "10.0.8.11:9092";
        //String topic = "my-topic";
        String topic = "call_center_message";
        //String topic = "call_log_record";

        Properties properties = new Properties();

        properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
        //指定key和value使用的序列化方式，消费者需要反序列化，才能得到原始的消息内容
        properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());

        //创建kafka生产者对象
        KafkaProducer<String, String> producer = new KafkaProducer<>(properties);

        String message = "{\"callid\":\"1ef39776-d2ed-11eb-b797-812cec3b0e62\",\"groupType\":\"2\",\"agentid\":\"4747c64a97094e3a85f95fee8a83d026\",\"agentGroupId\":\"0c588df3fd7a4476aa0c7dc75a646362_1\",\"satisfyLevel\":\"3\",\"agentName\":\"周勤\",\"fromattr\":\"浙江|杭州\",\"companyId\":\"0c588df3fd7a4476aa0c7dc75a646362\",\"messageType\":\"evaluateresult\",\"appId\":\"f5bf82d6673d4ec697165958c4795850\",\"startTime\":\"20210622080223570\",\"satisfyStr\":\"满意\",\"satisfyNumber\":\"1\",\"timestamp\":\"20210622080808790\",\"direction\":\"0\"}";

        //String message = "{\"callWay\":2,\"callee\":\"057128080500\",\"departmentId\":\"\",\"intervalEndTime\":0,\"platformType\":1,\"ender\":1,\"parentCallRecordId\":\"1ef39776-d2ed-11eb-b797-812cec3b0e62\",\"predictTaskId\":\"\",\"feeDuration\":0,\"robotMsgCount\":0,\"callType\":0,\"lastGroupName\":\"热线组\",\"callSrc\":0,\"id\":\"1ef39776-d2ed-11eb-b797-812cec3b0e62\",\"agentStartTime\":1624320176056,\"agentid\":\"4747c64a97094e3a85f95fee8a83d026\",\"leaveMsgUrl\":\"\",\"isLeaveMsg\":0,\"params\":\"\",\"taskDetailId\":\"\",\"unHold\":0,\"groupName\":\"热线组\",\"queueDuration\":0,\"disNumber\":\"057128080500\",\"agentAlertingTimeStr\":\"20210622080254003\",\"callResult\":10,\"isqueue\":0,\"callDuration\":342,\"userAlertingTime\":0,\"callDirection\":0,\"callFlag\":1,\"receiveStaffNames\":\"周勤,周勤\",\"outQueueTime\":0,\"lastStaffName\":\"周勤\",\"needToRecoverData\":false,\"lastGroupId\":\"0c588df3fd7a4476aa0c7dc75a646362_1\",\"extSatisfyType\":1,\"staffName\":\"周勤\",\"alertingTimeDuration\":2,\"startTime\":1624320143570,\"overflowFlag\":0,\"agentWaitDuration\":0,\"parentRecordFlag\":1,\"hiddenFlag\":0,\"satisfyLevel\":3,\"holdTime\":0,\"callSource\":0,\"voiceAliyunUrl\":\"https://img.sobot.com/pass/record/0c588df3fd7a4476aa0c7dc75a646362/f5bf82d6673d4ec697165958c4795850/20210622/1ef39776-d2ed-11eb-b797-812cec3b0e62.wav\",\"endTime\":1624320485984,\"listenStartTime\":0,\"abandonFlag\":0,\"passPlatformType\":1,\"satisfyNumber\":0,\"firstSolve24Hour\":1,\"numberType\":6,\"calleeId\":\"4747c64a97094e3a85f95fee8a83d026\",\"callTaskBridgeTime\":0,\"fee\":\"0.0\",\"groupId\":\"0c588df3fd7a4476aa0c7dc75a646362_1\",\"holdCount\":0,\"consultCount\":0,\"templateId\":\"\",\"transferCount\":0,\"hold\":0,\"breakDownSuccTime\":0,\"userAnswerDuration\":0,\"lastStaffId\":\"4747c64a97094e3a85f95fee8a83d026\",\"appId\":\"f5bf82d6673d4ec697165958c4795850\",\"lineType\":64,\"consultFlag\":0,\"callRecordId\":\"1ef39776-d2ed-11eb-b797-812cec3b0e62\",\"agentEndTime\":1624320485887,\"breakInSuccTime\":0,\"calleeName\":\"周勤\",\"companyId\":\"0c588df3fd7a4476aa0c7dc75a646362\",\"agentEndTimeStr\":\"20210622080805887\",\"taskCreateTime\":0,\"ivrTrajectoryDetail\":[{\"groupName\":\"热线组\",\"navActionId\":2,\"ivrName\":\"语音导航\",\"groupId\":\"0c588df3fd7a4476aa0c7dc75a646362_1\",\"keyId\":\"e1a70a999c334c1890e7076c5b2ab7f9\",\"navLevel\":2,\"navName\":\"理论\",\"parentId\":\"732371432f4a4af6bab4fafb92038a41\",\"navKey\":\"1\",\"timestamp\":\"20210622080246344\"}],\"transferFlag\":0,\"groupType\":2,\"isOutCallInTask\":0,\"listenFlag\":0,\"inQueueTime\":0,\"agentStartTimeStr\":\"20210622080256056\",\"callIds\":\"\",\"startTimeStr\":\"20210622080223570\",\"agentAlertingTime\":1624320174003,\"numberCity\":\"杭州\",\"duration\":309,\"enterGroupFlag\":1,\"taskType\":\"\",\"endtype\":1,\"userMsgCount\":0,\"hanupTime\":0,\"intervalDuration\":309,\"direction\":0,\"queueState\":1,\"serviceEmail\":\"1209331007@qq.com\",\"agentLoginType\":-1,\"caller\":\"15336523383\",\"templateName\":\"\",\"endTimeStr\":\"20210622080805984\",\"numberProvice\":\"浙江\",\"receiveStaffIds\":\"4747c64a97094e3a85f95fee8a83d026,4747c64a97094e3a85f95fee8a83d026\",\"staffId\":\"4747c64a97094e3a85f95fee8a83d026\",\"taskId\":\"\",\"userAnswerTime\":0}";

        try {
            while (true) {
                //producer.send(new ProducerRecord<String, String>(topic, "hello,java,hello,spark,hello,kafka"));
                producer.send(new ProducerRecord<String, String>(topic, message));

                Thread.sleep(2 * 1000L);
            }
            //producer.send(new ProducerRecord<String, String>(topic, "生产者测试1"));
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            //关闭
            producer.close();
        }
    }
}
